<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use HiCommon\Repository\InsuranceCompanySeedRepository;
use HiCommon\Repository\OrderCheckpointVehicleRepository;
use HiCommon\Repository\UsersVehicleInsuranceRepository;
use HiCommon\Repository\UsersVehicleRepository;
use HiCommon\Repository\VehicleModelAircondrefrigeratorRepository;
use HiCommon\Repository\VehicleModelBasicRepository;
use HiCommon\Repository\VehicleModelBodyRepository;
use HiCommon\Repository\VehicleModelChassisbrakeRepository;
use HiCommon\Repository\VehicleModelDoormirrorRepository;
use HiCommon\Repository\VehicleModelDrivingauxiliaryRepository;
use HiCommon\Repository\VehicleModelEngineRepository;
use HiCommon\Repository\VehicleModelEntcomRepository;
use HiCommon\Repository\VehicleModelGearboxRepository;
use HiCommon\Repository\VehicleModelInternalconfigRepository;
use HiCommon\Repository\VehicleModelLightRepository;
use HiCommon\Repository\VehicleModelRepository;
use HiCommon\Repository\VehicleModelSafeRepository;
use HiCommon\Repository\VehicleModelSeatRepository;
use HiCommon\Repository\VehicleModelWheelRepository;

class UsersVehicleService extends BaseService
{

    /**
     * 检测爱车档案表单数据
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function check_data_for_user_vehicle(&$arr_input){
        $arr_input['plate_no'] = str_trim($arr_input['plate_no']);
        $arr_input['vin'] = str_trim($arr_input['vin']);
        $arr_input['vehicle_type'] = str_trim($arr_input['vehicle_type']);
        $arr_input['owner'] = str_trim($arr_input['owner']);
        $arr_input['address'] = str_trim($arr_input['address']);
        $arr_input['use_character'] = str_trim($arr_input['use_character']);
        $arr_input['brand_model'] = str_trim($arr_input['brand_model']);
        $arr_input['engine_no'] = str_trim($arr_input['engine_no']);
        $arr_input['register_date'] = str_trim($arr_input['register_date']);
        $arr_input['issue_date'] = str_trim($arr_input['issue_date']);
        $arr_input['owner_phone'] = str_trim($arr_input['owner_phone']);

//        //拆分品牌型号=品牌+型号
//        $arr_model         = explode('牌',$arr_input['brand_model']);
//        $arr_input['brand'] =  str_trim($arr_model[0]);
//        $arr_input['model'] =  str_trim($arr_model[1]);

        if(mb_strlen($arr_input['plate_no']) <= 0){
            return hello_error('请填写车牌号码');
        }
        if(mb_strlen($arr_input['plate_no']) > 10){
            return hello_error('车牌号码不能超过10个字符');
        }
        if(mb_strlen($arr_input['vin']) <= 0){
            return hello_error('请填写车辆识别码');
        }
        if(mb_strlen($arr_input['vin']) > 20){
            return hello_error('车辆识别码不能超过20个字符');
        }
        if(mb_strlen($arr_input['vehicle_type']) <= 0){
            return hello_error('请填写车辆类型');
        }
        if(mb_strlen($arr_input['vehicle_type']) > 20){
            return hello_error('车辆类型不能超过20个字符');
        }
        if(mb_strlen($arr_input['owner']) <= 0){
            return hello_error('请填写所有人');
        }
        if(mb_strlen($arr_input['owner']) > 50){
            return hello_error('所有人不能超过50个字符');
        }
        if(mb_strlen($arr_input['address']) <= 0){
            return hello_error('请填写地址');
        }
        if(mb_strlen($arr_input['address']) > 100){
            return hello_error('地址不能超过100个字符');
        }
        if(mb_strlen($arr_input['use_character']) <= 0){
            return hello_error('请填写使用性质');
        }
        if(mb_strlen($arr_input['use_character']) > 50){
            return hello_error('使用性质不能超过50个字符');
        }
        if(mb_strlen($arr_input['brand_model']) <= 0){
            return hello_error('请填写品牌型号');
        }
        if(mb_strlen($arr_input['brand_model']) > 50){
            return hello_error('品牌型号不能超过50个字符');
        }
        if(mb_strlen($arr_input['engine_no']) <= 0){
            return hello_error('请填写发动机号码');
        }
        if(mb_strlen($arr_input['engine_no']) > 20){
            return hello_error('发动机号码不能超过20个字符');
        }
        if(mb_strlen($arr_input['register_date']) <= 0){
            return hello_error('请填写注册日期');
        }
        if(mb_strlen($arr_input['register_date']) > 10){
            return hello_error('注册日期不能超过10个字符');
        }
        if(mb_strlen($arr_input['issue_date']) <= 0){
            return hello_error('请填写发证日期');
        }
        if(mb_strlen($arr_input['issue_date']) > 10){
            return hello_error('发证日期不能超过10个字符');
        }
        return  hello_success('成功');
    }

    /**
     * 检测爱车档案表单数据-所有数据
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function check_data_for_whole_user_vehicle(&$arr_input){
        $arr_input['users_company_id'] = intval($arr_input['users_company_id']);
        $arr_input['vehicle_model_id'] = intval($arr_input['vehicle_model_id']);

        if(mb_strlen($arr_input['plate_no']) > 50){
            return hello_error('车牌号码不能超过50个字符');
        }
        if(mb_strlen($arr_input['vin']) > 50){
            return hello_error('车辆识别码不能超过50个字符');
        }
        if(mb_strlen($arr_input['vehicle_type']) > 50){
            return hello_error('车辆类型不能超过50个字符');
        }
        if(mb_strlen($arr_input['owner']) > 50){
            return hello_error('所有人不能超过50个字符');
        }
        if(mb_strlen($arr_input['owner_phone']) > 15){
            return hello_error('车主手机号不能超过15个字符');
        }
        if(mb_strlen($arr_input['address']) > 100){
            return hello_error('地址不能超过100个字符');
        }
        if(mb_strlen($arr_input['use_character']) > 50){
            return hello_error('使用性质不能超过50个字符');
        }
        if(mb_strlen($arr_input['brand_model']) > 50){
            return hello_error('品牌型号不能超过50个字符');
        }
        if(mb_strlen($arr_input['engine_no']) > 50){
            return hello_error('发动机号码不能超过50个字符');
        }
        if(mb_strlen($arr_input['register_date']) > 10){
            return hello_error('注册日期不能超过10个字符');
        }
        if(mb_strlen($arr_input['issue_date']) > 10){
            return hello_error('发证日期不能超过10个字符');
        }
        return  hello_success('成功');
    }

    /**
     * 添加用户车辆档案-仅含行驶证信息
     *
     * @param int   $n_uid
     * @param array $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_add_user_vehicle($n_uid = 0,$arr_input = []){
        $n_uid = intval($n_uid);
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        //检测表单数据
        $res = UsersVehicleService::check_data_for_user_vehicle($arr_input);
        if($res['state'] != 0){
            return $res;
        }

        $arr_input['is_default'] = 1;

        //非企业车辆，每次添加都会自动设置默认,其他车辆设置为非默认
        $arr_where = ['user_id'=>$n_uid];
        $arr_data = ['is_default'=>2];
        $n_count = UsersVehicleRepository::base_get_count_by_where($arr_where);
        if($n_count > 0){
            $res = UsersVehicleRepository::base_edit_by_where($arr_where,$arr_data);
            if($res == 0){
                return hello_error('添加失败',[],'添加车辆前，将其他车辆设置非默认车辆失败');
            }
        }

        //添加用户车辆档案
        $arr_input['user_id'] = $n_uid;
        $arr_input['vehicle_model_id'] = 100003;//TODO 小程默认车型
        $res = UsersVehicleRepository::base_add_one($arr_input)->toarray();
        if(empty($res)){
            return hello_error('添加失败');
        }
        return  hello_success('添加成功');
    }

    /**
     * 添加用户车辆档案-包括车款信息
     *
     * @param int   $n_uid
     * @param array $arr_input
     *
     * @return array|mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_add_whole_user_vehicle($n_uid = 0,$arr_input = []){
        $n_uid = intval($n_uid);
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }
        filter_input_data($arr_input);//过滤数组里的null字段，同时去掉字符串中的空格

        //检测表单数据
        $arr_input['user_id'] = $n_uid;
        $res = UsersVehicleService::check_data_for_whole_user_vehicle($arr_input);
        if($res['state'] != 0){
            return $res;
        }

        $arr_input['is_default'] = 2;

        if($arr_input['users_company_id'] == 0){
            $arr_input['is_default'] = 1;

            //非企业车辆，每次添加都会自动设置默认,其他车辆设置为非默认
            $arr_where = ['user_id'=>$n_uid];
            $arr_data = ['is_default'=>2];
            $n_count = UsersVehicleRepository::base_get_count_by_where($arr_where);
            if($n_count > 0){
                $res = UsersVehicleRepository::base_edit_by_where($arr_where,$arr_data);
                if($res == 0){
                    return hello_error('添加失败',[],'添加车辆前，将其他车辆设置非默认车辆失败');
                }
            }
        }

        //处理上传图片
        if(!empty($arr_input['driving_permit_img'])){
            $res = upload_single_file_by_base64($arr_input['driving_permit_img'],'user_vehicle/driving_permit_img');
            if($res['state'] != 0){
                return hello_error('行驶证上传失败');
            }
            $arr_input['driving_permit_img'] = $res['data']['src'];
        }


        //添加用户车辆档案
        $res = UsersVehicleRepository::base_add_one($arr_input)->toarray();
        if(empty($res)){
            return hello_error('添加失败');
        }
        return  hello_success('添加成功',$res);
    }

    /**
     * 修改车辆档案
     *
     * @param int   $n_uid
     * @param array $arr_input
     *
     * @return array|mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_edit_whole_user_vehicle($n_uid = 0,$arr_input = []){
        $n_uid = intval($n_uid);
        $arr_input['id'] = intval($arr_input['id']);
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }
        if($arr_input['id'] <= 0){
            return hello_error('数据id不能为空');
        }
        filter_input_data($arr_input);//过滤数组里的null字段，同时去掉字符串中的空格

        //检测表单数据
        $arr_input['user_id'] = $n_uid;
        $res = UsersVehicleService::check_data_for_whole_user_vehicle($arr_input);
        unset($arr_input['users_company_id']);
        unset($arr_input['is_default']);
        if($res['state'] != 0){
            return $res;
        }

        //处理上传图片
        $s_driving_permit_img_url = '';
        if(!empty($arr_input['driving_permit_img'])){
            $res = upload_single_file_by_base64($arr_input['driving_permit_img'],'user_vehicle/driving_permit_img');
            if($res['state'] != 0){
                return hello_error('行驶证上传失败');
            }
            $arr_input['driving_permit_img'] = $res['data']['src'];
            $s_driving_permit_img_url = $res['data']['url'];
        }

        //修改用户车辆档案
        $res = UsersVehicleRepository::base_edit_one($arr_input);
        if(!$res){
            return hello_error('修改失败');
        }
        $arr_input['driving_permit_img'] = $s_driving_permit_img_url;
        return  hello_success('修改成功',$arr_input);
    }


    /**
     * 修改用户车辆档案
     *
     * @param int   $n_uid
     * @param array $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_edit_users_vehicle($n_uid = 0,$arr_input = []){
        $n_uid = intval($n_uid);
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        //查询用户车辆档案
        $n_id = intval($arr_input['id']);
        if($n_id <= 0){
            return hello_error('数据id不能为空');
        }
        $res = UsersVehicleRepository::find($n_id,['id','user_id'])->toarray();
        if($res['user_id'] != $n_uid){
            return hello_error('只能修改自己的车辆档案');
        }

        //检测表单数据
        $res = UsersVehicleService::check_data_for_user_vehicle($arr_input);
        if($res['state'] != 0){
            return $res;
        }

        //修改用户车辆档案
        $res = UsersVehicleRepository::edit_one($arr_input);
        if(!$res){
            return hello_error('修改失败');
        }
        return  hello_success('修改成功');
    }

    /**
     * 删除用户车辆档案
     *
     * @param int $n_uid
     * @param int $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_del_user_vehicle($n_id = 0,$n_uid = 0){
        $n_uid = intval($n_uid);
        $n_id = intval($n_id);
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }
        if($n_id <= 0){
            return hello_error('数据id不能为空');
        }

        //删除用户车辆档案
        $result = UsersVehicleRepository::delete_one_users_vehicle($n_id);
        if($result == 0){
            return hello_error('删除失败');
        }
        //查询用户车辆档案列表是是否有默认车辆，若无 则设置默认车辆
        $arr_option = [
          'where'=>['users_company_id'=>0,'user_id'=>$n_uid,'status'=>1],
          'field'=>['id','user_id','is_default','users_company_id'],
          'order'=>['key'=>'id','value'=>'desc'],

        ];
        $arr_user_vehicle = UsersVehicleRepository::base_get_all($arr_option)->toarray();
        if(count($arr_user_vehicle) >= 1){
            $res_defalut = collect($arr_user_vehicle)->where('is_default',1)->first();
            if(is_null($res_defalut)){
                $res_this = collect($arr_user_vehicle)->where('is_default',2)->sortByDesc('id')->first();
                //设置默认
                $n_id =$res_this['id'];

                $arr_data = ['id'=>$n_id,'is_default'=>1];
                $res = UsersVehicleRepository::base_edit_one($arr_data);
                if(!$res ){
                    return hello_error('删除失败1');
                }
            }
        }

        return  hello_success('删除成功');
    }

    /**
     * 根据条件更新或者在创建一条数据
     *
     * @param $n_uid
     * @param $arr_data_vehicle
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function update_or_create($n_uid,$arr_data_vehicle){
        $arr_where = [
          'plate_no'=>$arr_data_vehicle['plate_no'],
          'user_id'=>$n_uid,
        ];
        $arr_data = [
          'vin'=>$arr_data_vehicle['vin'],
          'vehicle_type'=>$arr_data_vehicle['vehicle_type'],
          'owner'=>$arr_data_vehicle['owner'],
          'owner_phone'=>$arr_data_vehicle['owner_phone'],
          'address'=>$arr_data_vehicle['address'],
          'use_character'=>$arr_data_vehicle['use_character'],
          'brand_model'=>$arr_data_vehicle['brand_model'],
//          'brand'=>$arr_data_vehicle['brand'],
//          'model'=>$arr_data_vehicle['model'],
          'engine_no'=>$arr_data_vehicle['engine_no'],
          'register_date'=>$arr_data_vehicle['register_date'],
          'issue_date'=>$arr_data_vehicle['issue_date'],
          'car_img'=>str_trim($arr_data_vehicle['car_img']),
        ];
        $res = UsersVehicleRepository::edit_or_create($arr_where,$arr_data)->toarray();
        if(empty($res)){
            return hello_error('操作失败');
        }
        return  hello_success('成功');
    }



//    /**
//     * 用户车辆档案列表
//     *
//     * @return array
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function api_list_by_uid($n_uid)
//    {
//        $arr_option = [
//          'where'=>['user_id'=>$n_uid,'status'=>1],
////          'field'=>['id','car_img','plate_no','vehicle_type','brand','model']
//        ];
//        $res = UsersVehicleRepository::base_get_all($arr_option)->toarray();
//        return  hello_success('成功',$res);
//    }


    /**
     * 设置用户默认车辆
     *
     * @param int $n_id
     * @param int $n_uid
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_set_default_user_vehicle($n_id = 0,$n_uid = 0){
        $n_id = intval($n_id);
        $n_uid = intval($n_uid);
        if($n_id <= 0){
            return hello_error('车辆id不能为空');
        }
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        //查询车辆档案信息
        $res = UsersVehicleRepository::base_find_one($n_id,['field'=>['id','user_id','users_company_id','is_default']]);
        if(is_null($res)){
            return hello_error('数据不存在');
        }
        if($res->user_id != $n_uid){
            return hello_error('只能设置自己的车辆');
        }
        if($res->users_company_id != 0){
            return hello_error('企业车辆不能设置默认');
        }
        if($res->is_default == 1){
            return hello_error('车辆已设置默认，请勿重复操作');
        }

        //其他车辆先设置成非默认
        $arr_where = ['user_id'=>$n_uid];
        $arr_data = ['is_default'=>2];
        $res = UsersVehicleRepository::base_edit_by_where($arr_where,$arr_data);
        if($res == 0){
            return hello_error('设置失败');
        }

        //设置默认车辆
        $arr_data = ['id'=>$n_id,'is_default'=>1];
        $res = UsersVehicleRepository::base_edit_one($arr_data);
        if($res == 0){
            return hello_error('设置失败');
        }
        return  hello_success('设置成功');
    }


    /**
     * 用户非企业车辆列表
     *
     * @param int $n_uid
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_user_vehicle_list($n_uid = 0){
        $n_uid = intval($n_uid);
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        $res = UsersVehicleRepository::get_all_by_uid($n_uid)->toarray();
        foreach ($res as &$value)
        {
            $value['vehicle_model'] = $value['simple_vehicle_model'];
            unset($value['simple_vehicle_model']);
        }

        return  hello_success('成功',$res);
    }

    /*我的车库*/
    public function api_my_user_vehicle($n_uid = 0){
        $n_uid = intval($n_uid);
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        $arr_data = [];

        //我的车辆
        $res = self::api_user_vehicle_list($n_uid);
        if($res['state'] != 0){
            return $res;
        }
        $arr_data['vehicle'] = $res['data'];

        //我的企业
        $res = UsersCompanyService::api_get_user_company_list($n_uid);
        if($res['state'] != 0){
            return $res;
        }
        $arr_data['company'] = $res['data'];
        return  hello_success('成功',$arr_data);
    }

    public function update_or_create_vehicle_model($n_id,$arr_car_detail){
        //更新主表信息
        $arr_remain_key = ['id','name','initial','parentid','logo','price','yeartype','productionstate','salestate','sizetype','depth'];
        $arr_data = array_remain($arr_car_detail,$arr_remain_key);
        filter_input_data($arr_data);
        $arr_where = ['id'=>$n_id];
        $arr_data['is_need_update_self'] = 2;
        VehicleModelRepository::base_update_or_create($arr_where,$arr_data);
    }

    public function update_or_create_vehicle_model_info($n_id,$arr_car_detail){

        //更新主表信息
        $arr_where = ['id'=>$n_id];
        $arr_data = ['is_need_update_info'=>2];
        VehicleModelRepository::base_update_or_create($arr_where,$arr_data);

        $arr_where = ['vehicle_model_id'=>$n_id];

        //basic
        $arr_data = $arr_car_detail['basic'];
        filter_input_data($arr_data);
        VehicleModelBasicRepository::base_update_or_create($arr_where,$arr_data);

        //body
        $arr_data = $arr_car_detail['body'];
        filter_input_data($arr_data);
        VehicleModelBodyRepository::base_update_or_create($arr_where,$arr_data);

        //engine
        $arr_data = $arr_car_detail['engine'];
        filter_input_data($arr_data);
        VehicleModelEngineRepository::base_update_or_create($arr_where,$arr_data);

        //gearbox
        $arr_data = $arr_car_detail['gearbox'];
        filter_input_data($arr_data);
        VehicleModelGearboxRepository::base_update_or_create($arr_where,$arr_data);

        //chassisbrake
        $arr_data = $arr_car_detail['chassisbrake'];
        filter_input_data($arr_data);
        VehicleModelChassisbrakeRepository::base_update_or_create($arr_where,$arr_data);

        //safe
        $arr_data = $arr_car_detail['safe'];
        filter_input_data($arr_data);
        VehicleModelSafeRepository::base_update_or_create($arr_where,$arr_data);

        //wheel
        $arr_data = $arr_car_detail['wheel'];
        filter_input_data($arr_data);
        VehicleModelWheelRepository::base_update_or_create($arr_where,$arr_data);

        //drivingauxiliary
        $arr_data = $arr_car_detail['drivingauxiliary'];
        filter_input_data($arr_data);
        VehicleModelDrivingauxiliaryRepository::base_update_or_create($arr_where,$arr_data);

        //doormirror
        $arr_data = $arr_car_detail['doormirror'];
        filter_input_data($arr_data);
        VehicleModelDoormirrorRepository::base_update_or_create($arr_where,$arr_data);

        //light
        $arr_data = $arr_car_detail['light'];
        filter_input_data($arr_data);
        $arr_where = ['vehicle_model_id'=>$n_id];
        VehicleModelLightRepository::base_update_or_create($arr_where,$arr_data);

        //internalconfig
        $arr_data = $arr_car_detail['internalconfig'];
        filter_input_data($arr_data);
        VehicleModelInternalconfigRepository::base_update_or_create($arr_where,$arr_data);

        //seat
        $arr_data = $arr_car_detail['seat'];
        filter_input_data($arr_data);
        VehicleModelSeatRepository::base_update_or_create($arr_where,$arr_data);

        //entcom
        $arr_data = $arr_car_detail['entcom'];
        filter_input_data($arr_data);
        VehicleModelEntcomRepository::base_update_or_create($arr_where,$arr_data);

        //aircondrefrigerator
        $arr_data = $arr_car_detail['aircondrefrigerator'];
        filter_input_data($arr_data);
        VehicleModelAircondrefrigeratorRepository::base_update_or_create($arr_where,$arr_data);
    }

    public function update_or_create_vehicle_model_and_info($n_id = 0){
        $res = VehicleModelRepository::base_find_one($n_id)->toarray();
        if($res['is_need_update_self'] == 1 || $res['is_need_update_info'] == 1){
            $car_detail = json_decode(jisu_car_detail($n_id),true);
            if($car_detail['status'] == 0){
                $arr_car_detail = $car_detail['result'];
            }

            if($res['is_need_update_self'] == 1){
                self::update_or_create_vehicle_model($n_id,$arr_car_detail);
            }

            if($res['is_need_update_info'] == 1){
                //更新附属多张子表信息
                self::update_or_create_vehicle_model_info($n_id,$arr_car_detail);
            }
        }
    }

    /*用户车辆信息*/
    public function api_vehicle_info($n_id = 0){
        $n_id = intval($n_id);
        if($n_id <= 0){
            return hello_error('车辆id不能为空');
        }
        $res = UsersVehicleRepository::base_find_one($n_id);
        if(is_null($res)){
            return hello_error('数据不存在');
        }

        //更新车型详情信息
        $arr_user_vehicle = $res->toarray();
        $n_vehicle_model_id = $arr_user_vehicle['vehicle_model_id'];
        self::update_or_create_vehicle_model_and_info($n_vehicle_model_id);

        //查询车型详情信息
        $arr_option                     = [
          'with'  => ['simple_vehicle_model','vehicle_model.basic','users_vehicle_insurance.insurance_company'],
        ];
        $arr_user_vehicle = UsersVehicleRepository::base_find_one($n_id, $arr_option)
                                                  ->toarray();
        $arr_base = $arr_user_vehicle['simple_vehicle_model'];
        $arr_basic = $arr_user_vehicle['vehicle_model'];
        $arr_insurance = $arr_user_vehicle['users_vehicle_insurance'];

        //转换数据，筛选有用的数据
        $arr_data['id'] = $arr_user_vehicle['id'];//数据id

        //车辆基础信息
        $arr_data['base'] = $arr_base;

        //基本信息
        $arr_data['basic'] = [
          'displacement'=>$arr_basic['basic']['displacement'],
          'yeartype'=>$arr_basic['yeartype'],
          'name'=>$arr_basic['name'],
          'mileage'=>$arr_user_vehicle['mileage'],
          'departure_date'=>$arr_user_vehicle['departure_date'],
          'register_date'=>$arr_user_vehicle['register_date']
        ];

        //违章信息
        $arr_data['break_rule'] = [
            'plate_info'=>plate_city($arr_user_vehicle['plate_no']),
            'plate_pre'=>mb_substr($arr_user_vehicle['plate_no'],0,1),
            'plate_no'=>$arr_user_vehicle['plate_no'],
            'vin'=>$arr_user_vehicle['vin'],
            'engine_no'=>$arr_user_vehicle['engine_no']
        ];

        //保险信息
        $arr_option = [
          'where'=>['insurance_company_id'=>$arr_insurance['insurance_company_id']],
          'field'=>['id','name']
        ];

        $arr_insurance_seed = InsuranceCompanySeedRepository::base_get_all($arr_option);
        $arr_insurance_seed_ids = explode(',',str_replace('seed_','',$arr_insurance['insurance_company_seed_ids']));
        $arr_this_insurance_seed = array_values($arr_insurance_seed->whereIn('id',$arr_insurance_seed_ids)->toarray());
        $arr_data['insurance_info'] = [
          'insurance_company'=>$arr_insurance['insurance_company'],
          'insurance_expire_date'=>$arr_insurance['insurance_expire_date'],
          'vehicle_owner'=>$arr_user_vehicle['owner'],
          'vehicle_owner_id_card'=>$arr_insurance['vehicle_owner_id_card'],
          'insurance_city'=>$arr_insurance['insurance_city'],
          'is_transfer'=>$arr_insurance['is_transfer'],
          'insurance_seed'=>$arr_this_insurance_seed,
          'insurance_seed_count'=>count($arr_this_insurance_seed),
        ];

        return  hello_success('成功',$arr_data);
    }

    /**
     * 修改车辆基本信息
     *
     * @param array $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_edit_vehicle_basic($arr_input = []){

//        $arr_input['mileage'] = str_trim($arr_input['mileage']);
//        $arr_input['departure_date'] = str_trim($arr_input['departure_date']);

        $arr_input['id'] = intval($arr_input['id']);
        $arr_input = array_remain($arr_input,['id','mileage','departure_date']);
        if($arr_input['id'] <= 0){
            return hello_error('失败',[],'数据id不能为空');
        }
        $result = UsersVehicleRepository::base_edit_one($arr_input);
        if($result === false){
            return hello_error('失败',[],'修改失败');
        }
        return  hello_success('成功');
    }

    /**
     * 修改车辆 保险信息
     *
     * @param array $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_vehicle_insurance_info($arr_input = []){
        $n_users_vehicle_id = intval($arr_input['id']);
        $arr_input = array_remain($arr_input,['insurance_company_id','insurance_company_seed_ids','insurance_expire_date','vehicle_owner_id_card','insurance_city','is_transfer']);
        $arr_insurance_company_seed_ids = $arr_input['insurance_company_seed_ids'];
        $arr_input['vehicle_owner_id_card'] = str_trim($arr_input['vehicle_owner_id_card']);

        if($n_users_vehicle_id <= 0){
            return hello_error('失败',[],'用户车辆id不能为空');
        }

        if(is_array($arr_insurance_company_seed_ids) && count($arr_insurance_company_seed_ids) > 0){
            $arr_insurance_company_seed_ids = arr_value_add_string($arr_insurance_company_seed_ids,'seed_');
            $s_insurance_company_seed_ids = implode(',',$arr_insurance_company_seed_ids);
            $arr_input['insurance_company_seed_ids'] = $s_insurance_company_seed_ids;
        }

        $arr_where = ['users_vehicle_id'=>$n_users_vehicle_id];

        $result = UsersVehicleInsuranceRepository::base_update_or_create($arr_where,$arr_input);
        if(empty($result->toarray())){
            return hello_error('失败',[],'修改失败');
        }
        return  hello_success('成功');
    }

    /*用户车辆详情*/
    public function api_vehicle_detail($n_id = 0){
        $n_id = intval($n_id);
        if($n_id <= 0){
            return hello_error('车辆id不能为空');
        }
        //查询车型详情信息
        $arr_option                     = [
          'with'  => ['whole_vehicle_model'],
        ];
        $res = UsersVehicleRepository::base_find_one($n_id,$arr_option);
        if(is_null($res)){
            return hello_error('数据不存在');
        }
        $arr_data = $res->toarray();
        $arr_base = $arr_data['whole_vehicle_model'];

        //车辆基础信息
        $arr_data = [
          'id'=>$arr_data['id'],
          'one_name'=>$arr_base['parent']['parent']['name'],
          'two_name'=>$arr_base['parent']['name'],
          'three_name'=>$arr_base['name'],
          'logo'=>$arr_base['parent']['parent']['parent']['logo'],
          'car_img'=>$arr_data['car_img']['full_src'],
          'driving_permit_img'=>$arr_data['driving_permit_img']['full_src'],
          'plate_no'=>$arr_data['plate_no'],
          'vin'=>$arr_data['vin'],
          'vehicle_type'=>$arr_data['vehicle_type'],
          'owner'=>$arr_data['owner'],
          'owner_phone'=>$arr_data['owner_phone'],
          'address'=>$arr_data['address'],
          'use_character'=>$arr_data['use_character'],
          'brand_model'=>$arr_data['brand_model'],
          'engine_no'=>$arr_data['engine_no'],
          'register_date'=>$arr_data['register_date'],
          'issue_date'=>$arr_data['issue_date'],
        ];

        return  hello_success('成功',$arr_data);
    }

    /*用户默认车辆信息*/
    public function api_user_default_car($n_uid = 0){
        if($n_uid <= 0){
            return hello_error('用户ID不能为空');
        }

        //查询用户默认车辆信息
        $arr_user_default_vehicle = null;
        $arr_option = [
          'where'=>['user_id'=>$n_uid,'users_company_id'=>0,'status'=>1,'is_default'=>1],
//          'field'=>['id','user_id','vehicle_model_id','plate_no','vin','engine_no'],
          'with'=>['simple_vehicle_model'],
        ];
        $result = UsersVehicleRepository::base_get_all($arr_option)->toarray();

        if(count($result) > 0){
            $arr_user_default_vehicle = $result[0];
            $arr_user_default_vehicle['vehicle_model'] = $arr_user_default_vehicle['simple_vehicle_model'];
            unset( $arr_user_default_vehicle['simple_vehicle_model'] );
        }

        return  hello_success('成功',$arr_user_default_vehicle);

    }

    /*企业车辆检测记录*/
    public function api_company_vehicle_check_records($n_company_id = 0,$s_year_month = ''){
        $firstday = date('Y-m-01', strtotime($s_year_month));
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        $arr_checked_at = [
          'start'=>$firstday,
          'end'=>$lastday,
        ];

        $result = OrderCheckpointVehicleRepository::company_vehicle_check_records($n_company_id,$arr_checked_at)->toArray();
        $arr_data = [];
        foreach ($result as $value)
        {
            $arr_user_vehicle = $value['user_vehicle'];
            $arr_vehicle_model = $arr_user_vehicle['simple_vehicle_model'];
            $arr_user_vehicle['vehicle_model'] = $arr_vehicle_model;
            unset($arr_user_vehicle['simple_vehicle_model']);
            $arr_data[] = [
              'id'=>$value['id'],
              'users_vehicle_id'=>$value['users_vehicle_id'],
              'check_state'=>$value['check_state'],
              'checked_at'=>$value['checked_at'],
//              'checked_date'=>$value['checked_date'],
              'user_vehicle'=>$arr_user_vehicle,

            ];
        }

        return  hello_success('成功',$arr_data);
    }
}